/*我的审核-- 初审修改页 */
<template>
  <div>
    <Card :bordered="false">
      <p slot="title">初审修改页</p>
      <table style="margin-bottom:20px" cellspacing="0" cellpadding="0" border="1" width="100%" ref="print">
        <tr>
          <th colspan="6">
            初审修改
          </th>
        </tr>
        <tr>
          <td width="25%">
            修改人员
          </td>
          <td width="25%" class="sizeStyle">
            {{info.RealName}}
          </td>
          <td width="25%">
            修改完成时间
          </td>
          <td class="sizeStyle">
            {{info.ModifierDotime}}
          </td>
        </tr>
      </table>
      <table style="margin-bottom:20px" cellspacing="0" cellpadding="0" border="1" width="100%">
        <tr>
          <th>
            初审修改清单
          </th>
        </tr>
        <tr>
          <td>
            <div style="width:99%;margin:auto;height:100%;">
              <Table style="border: none" border ref="selection" :columns="chushen" :data="info.data"></Table>
              <Row>
                <Col span="24" style="text-align: center;padding:10px">
                <Button type="primary" @click="passCS">通过初审</Button>
                </Col>
              </Row>
            </div>
          </td>
        </tr>
      </table>
      <table style="margin-bottom:20px" cellspacing="0" cellpadding="0" border="1" width="100%">
        <tr>
          <th colspan="4">
            初审修改清单2
          </th>
        </tr>
        <tr>
          <td width="25%" class="ivu-table-cell">初审工程师</td>
          <td width="25%" class="sizeStyle">
            {{info.Audit}}
          </td>
          <td width="25%">初审本次意见时间</td>
          <td width="25%" class="sizeStyle">
            {{this.$moment().format('YYYY-MM-DD')}}
          </td>
        </tr>
        <tr>
          <td colspan="4">
            <div style="width:99%;margin:auto;height:100%;">
              <Table id="ChushenXg" style="border: none" border ref="selection" :columns="chushenColumns" :data="ChushenXg"
                @on-row-click="getIndex"></Table>
              <Row>
                <Col span="24" style="text-align: center;padding:10px">
                <Button type="primary" @click="inputAdd('ChushenXg')">添加</Button>
                <Button type="error" @click="inputRemove('ChushenXg')">删除</Button>
                </Col>
              </Row>
            </div>
          </td>
        </tr>
        <tr>
          <td>
            报告（正文、附图、附件、附表）
          </td>

          <td colspan="3">
            <Button :disabled='!ProjectEnvOverview?true:false'  :to="ProjectEnvOverview" target='_blank' style="float:left;margin-left:50px">下载</Button>
            <div>
              <Upload with-credentials :before-upload="upLoadFun" :data='imgUpload' :action="shishi" ref="uploadButton">
                <Button>上传</Button>
              </Upload>
            </div>
          </td>

        </tr>
      </table>

      <Row style="padding:40px 0">
        <Col span="12" style="text-align: left">
        <!-- <Button type="primary" @click="$print($refs.print)">打印</Button> -->
        <PrintButton  Url="print/printAuditForm" :Project_id="info.Id || $route.query.ProjectId" Atype="初审"></PrintButton>
        </Col>
        <Col span="12" style="text-align: right;">
        <Button type="primary" @click="submit('')">保存</Button>
        <Button type="primary" @click="submit(2)">发送意见</Button>
        </Col>
      </Row>

    </Card>
  </div>
</template>

<script>
import libs from "../../../libs/uploads";
  export default {
    data() {
      return {
        shishi:'',
        imgUpload: {
          Id: this.$route.query.ProjectId,
          Key: 'ImgUrl'
        },
        info: {},
        query: [],
        chushenLoading: true,
        ChushenXg: [],
        chushen: [{
            align: "center",
            type: "selection",
            width: 60,
            align: "center"
          },
          {
            align: "center",
            width: 80,
            title: "序号",
            type: 'index',
          },
          {
            align: "center",
            title: "初审意见",
            key: "AuditOpinion"
          },
          {
            align: "center",
            title: "修改说明",
            key: "ReviseExplain"
          },
        ],
        chushenColumns: [{
            align: 'center',
            title: '序号',
            type: 'index',
            width: 80,
          },
          {
            align: 'center',
            title: '初审意见',
            key: 'AuditOpinion',
            render: (h, {
              row,
              index
            }) => {
              return h('Input', {
                props: {
                  type: 'textarea',
                  autosize: true,
                  value: row.AuditOpinion
                },
                on: {
                  'on-blur': (val) => {
                    this.ChushenXg[index].AuditOpinion = val.target.value
                    console.log('失去焦点查看：' + this.ChushenXg[index].AuditOpinion)
                  }
                }
              })
            }
          },
          // {
          //   align: 'center',
          //   title: '修改说明',
          //   key: 'Revise_explain',
          // }
        ],
        ProjectEnvOverview: '',
        proType: 0,
        outTime: 0,
        csPass: 0,
      };
    },
    mounted() {
      let query = this.$route.query
      this.query = query
      this.findByProject(query.ProjectId, query.Atype)
      let delinfo = ''
      this.shishi = libs.uploads();
    },
    methods: {
      inputAdd(type) {
        this.ChushenXg.push({AuditTime:new Date().getTime()})
      },
      inputRemove(type) {
        // console.log(type)
        // console.log(this[type])
        // console.log(this.allIndex)
        // 保存回显，点删除要删除数据库里面，新删除不需要
        var delId = this[type][this.allIndex].Id
        if(this.delinfo == '审核中'){
          if(delId) {
            axios.post('/auditreply/deleteByid',{Id:delId}).then(res=>{
              if(res._STATUS==1) {
                this.$Message.success('删除成功！')
                this[type].splice(this.allIndex, 1); //删除数据库后本地也删除
              }else {
                this.$Message.error(res._MESSAGE)
              }
            })
          }else {
            this[type].splice(this.allIndex, 1);
            console.log(this[type] + ' 被删除了')
          } 
        } else if (this.delinfo == '未提交'){
          alert('审核未提交，无法进行修改')
          return
        } else if (this.delinfo == '已审核'){
          alert('审核已结束，无法进行修改')
          return
        }
      },
      getIndex(data, index, event) {
        this.allIndex = index;
      },
      findByProject(ProjectId, Atype) {
        // axios.get('/audit/findByProject2', {
        this.$post('audit.findByProject2', {
            Atype,
            ProjectId
        }).then(res => {
          // 把点保存时 填写数据过滤出来,初审修改清单中不会显示
          if(res.data&&res.data.length>0) {
            res.data = res.data.filter((ele)=>{
              if(ele.ChangeTime==null&&ele.ReviseExplain==null) {
                this.ChushenXg.push(ele)
              }
              return ele.ChangeTime!=null&&ele.ReviseExplain!=null
            })
          }
          this.getDownInfo()
          this.info = res
          this.delinfo = res.Stats
          console.log(res)
          var number = this.info.AuditTime.length + 1
          this.imgUpload.Name = '初审' + number + '批'
          this.chushenLoading = false
          if(res.Stats == '审核中'){
            this.proType = 1
          }
        })
      },
      getDownInfo(){
        axios.post("wechatpublic/findImg",{
          Id:this.$route.query.ProjectId
        }).then((res)=>{
          if(this.info.Stats == '未提交'){
            this.ProjectEnvOverview =''
          }else{
            for(var i=0;i<res.data.length;i++){
              if(res.data[i].Status == '初审'){
                this.ProjectEnvOverview = res.data[i].ImgUrl
              }
            }
          }
        })
      },
      upLoadFun(){
        this.$refs.uploadButton.clearFiles()
        if(this.proType != 1){
          this.$Message.warning('审核未开始，无法上传文件')
          return false
        }
      },
      submit(type) {
        if(this.outTime == 0){
          this.outTime = 1
          var bl = false;
          setTimeout(()=>{this.outTime = 0},5000)
          let query = this.query
          let AuditReplyLists = this.ChushenXg
          AuditReplyLists.map(i=>{
            if(i.AuditOpinion && i.AuditOpinion != ''){
              
            }else{
              bl = true
            }
          })
          if(bl){
            this.outTime = 0
            alert('请填写完整意见后，再发送意见！')
            return
          }

          var dataArr = Object.keys(AuditReplyLists).length <=0 ? [] : '123'
          for (let val of AuditReplyLists) {
            val['AuditId'] = query.Id
            // val['Audit_time'] = this.$moment().format('YYYY-MM-DD HH:mm:ss')
          }
          axios.get('/audit/isAuditb', {
            params: {
              ProjectId: query.ProjectId,
              Btype: query.Atype,
              OK: 1,
              Save: type,
              AuditReplyLists: dataArr,
            }
          }).then(res => {
            let title = res._MESSAGE
            if (res._STATUS == 1) {
              this.$Modal.success({
                title: title 
              })
              if(type == 2){
              social({
                method: 'post',
                url: '/info/notice',
                params: {
                  content: this.info.ProjectName +'项目初审工程师提出审核意见，请尽快进行修改！',
                  app_name: '环评管控系统',
                  app: 'eao',
                  type: this.info.StaffNamesId +'',
                  uid: -1
                },
                }).then((res)=>{
                  console.log(res)
                }).catch((err)=>{
                  console.log(err)
                })
              }
              axios.post('/auditreply/saveOrUpdateM', {
                AuditReplyLists:JSON.stringify(AuditReplyLists),
                ProjectId: query.ProjectId,
                Atype: query.Atype,
                Aoc: '2',
                Save: type
              }).then(res => {
                console.log(res)
                // 保存、发送意见成功后跳转到正在审核页面
                this.$router.push({name: 'myProjectCheck'})
              }).catch(err => {
                this.$Modal.error({
                  title: '消息发送失败'
                })
              })
            } else {
              this.$Modal.error({
                title,
              })
            }
          })
        }else{
          console.log('别点了老铁')
        }
      },
      passCS() {
        if(this.csPass == 0){
          this.csPass = 1
          setTimeout(()=>{this.csPass = 0},5000)
          axios.get('/audit/isAuditb', {
            params: {
              ProjectId: this.$route.query.ProjectId,
              Btype: this.$route.query.Atype,
              OK: 0,
              AuditReplyLists: '123'
            }
          }).then(res => {
            let title = res._MESSAGE
            console.log(res)
            if (res._STATUS == 1) {
              /**  项目成员消息*/
              social({
                method: 'post',
                url: '/info/notice',
                params: {
                  content: this.info.ProjectName +'项目已通过初审工程师审核，已经递交到复审！',
                  app_name: '环评管控系统',
                  app: 'eao',
                  type: this.info.StaffNamesId + '',
                  uid: -1
                },
                }).then((res)=>{
                  console.log(res)
                }).catch((err)=>{
                  console.log(err)
              })
              /**  项目负责人消息*/
              social({
                method: 'post',
                url: '/info/notice',
                params: {
                  content: this.info.ProjectName +'项目已通过你的审核，已经递交到复审！',
                  app_name: '环评管控系统',
                  app: 'eao',
                  type: this.info.auditor + '',
                  uid: -1
                },
                }).then((res)=>{
                  console.log(res)
                }).catch((err)=>{
                  console.log(err)
              })
              this.$Modal.success({
                title: title,
                onOk:()=>{
                  this.$router.push({name: 'myProjectCheck',params: {success:1}})
                }
              })
            } else {
              this.$Modal.error({
                title
              })
            }
          })
        }
      },

    }
  };
</script>

<style lang="less" scoped>
  #ChushenXg {
    .ivu-input {
      border: none !important;
      height: 30px;

      input {
        outline: medium;
      }
    }

    .ivu-table-tip {
      overflow-x: hidden;
    }
  }
  /deep/ textarea.ivu-input {
    font-size: 12px;
  }
  .sizeStyle {
    font-size:12px;
  }
</style>